﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;

namespace Medianamik.UI.Controls
{
    //TODO: Nice to have...
    public class OrdedList : BaseMedianamikControl
    {
        ListBox _listBox = new ListBox() { ID = ControlToValidateId };

        protected void lUp_Click(object sender, EventArgs e)
        {
            if (_listBox.SelectedIndex > 0)
            {
                int lastIndex = _listBox.SelectedIndex;
                var oldLI = _listBox.Items[lastIndex];
                _listBox.Items.RemoveAt(lastIndex);
                _listBox.Items.Insert(lastIndex - 1, oldLI);
            }
        }

        protected void lDown_Click(object sender, EventArgs e)
        {
            if (_listBox.SelectedIndex > -1 && _listBox.SelectedIndex < _listBox.Items.Count - 1)
            {
                int lastIndex = _listBox.SelectedIndex;
                var oldLI = _listBox.Items[lastIndex];
                _listBox.Items.RemoveAt(lastIndex);
                _listBox.Items.Insert(lastIndex + 1, oldLI);
            }
        }

        protected void lDelete_Click(object sender, EventArgs e)
        {
            if (_listBox.SelectedIndex > -1)
                _listBox.Items.RemoveAt(_listBox.SelectedIndex);
        }

        protected void lAdd_Click(object sender, EventArgs e)
        {
            var ddlSelection = (System.Web.UI.WebControls.DropDownList)FindControl("ddlSelection");

            if (_listBox.Items.FindByValue(ddlSelection.SelectedValue) == null)
            {
                var item = new CsmPartner(new Guid(ddlSelection.SelectedValue), "Fr-Ca");
                _listBox.Items.Add(new ListItem(item.Name, item.NodeId.ToString()));
            }
        }

        protected override void CreateControlHierarchy(bool pUseDataSource)
        {
            var value = DataSource as string ?? string.Empty;

            _listBox.Height = 300;
            _listBox.Width = 500;

            // Ajout des valeurs sélectionnées.
            _listBox.CssClass = "SelectList";
            _listBox.DataSource = new CsmPartnerCollection().LoadByIDs(value);
            _listBox.DataTextField = CsmPartner.Columns.Name;
            _listBox.DataValueField = CsmPartner.Columns.NodeId;
            _listBox.DataBind();

            Controls.Add(_listBox);
            Controls.Add(new System.Web.UI.LiteralControl("<br />"));

            // Bouton Monter
            var lUp = new Button();
            lUp.Text = "Monter la sélection";
            lUp.CssClass = "Btn";
            lUp.Click += lUp_Click;

            Controls.Add(lUp);
            Controls.Add(new System.Web.UI.LiteralControl("&nbsp;"));

            // Bouton Descendre
            var lDown = new Button();
            lDown.Text = "Descendre la sélection";
            lDown.CssClass = "Btn";
            lDown.Click += lDown_Click;

            Controls.Add(lDown);
            Controls.Add(new System.Web.UI.LiteralControl("&nbsp;"));

            // Bouton soupprimer
            var lDelete = new Button();
            lDelete.Text = "Supprimer la sélection";
            lDelete.CssClass = "Btn";
            lDelete.Click += lDelete_Click;

            Controls.Add(lDelete);
            Controls.Add(new System.Web.UI.LiteralControl("&nbsp;"));

            Controls.Add(new System.Web.UI.LiteralControl("<br /><hr />"));

            // On ajoute une dropdown en dessous
            var ddlSelection = new System.Web.UI.WebControls.DropDownList();
            ddlSelection.ID = "ddlSelection";
            ddlSelection.CssClass = "SelectList";
            ddlSelection.DataSource = new CsmPartnerCollection("Fr-Ca").Load().OrderBy(p => p.Name);
            ddlSelection.DataTextField = CsmPartner.Columns.Name;
            ddlSelection.DataValueField = CsmPartner.Columns.NodeId;
            ddlSelection.DataBind();

            Controls.Add(ddlSelection);
            Controls.Add(new System.Web.UI.LiteralControl("&nbsp;"));

            // Bouton ADD
            var lAdd = new Button();
            lAdd.Text = "Ajout à la liste";
            lAdd.CssClass = "Btn";
            lAdd.Click += lAdd_Click;

            Controls.Add(lAdd);
        }

        public override object GetValue()
        {
            EnsureChildControls();

            var result = new List<string>();
            for (var i = 0; i < _listBox.Items.Count; i++)
                result.Add(_listBox.Items[i].Value);

            return string.Join(",", result.ToArray());
        }

        public override Type TypeOfValue
        {
            get { return typeof(string); }
        }
    }
}
